package com.lbd.banner.contoller;

import com.lbd.banner.bean.Banner;
import com.lbd.banner.service.BannerService;
import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by zzl on 2016/9/23.
 */
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {
    @Autowired
    private BannerService bannerService;

    /**
     * 按条件分页查询banner
     *
     * @param page   第几页
     * @param rows   一页多少条
     * @param banner
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Banner banner) {
        return new Result(ResultState.SUCCESS, "查询成功", bannerService.findByPageConditionJpql(page, rows, banner));
    }

    /**
     * 保存banner信息
     *
     * @param banner
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Banner banner, HttpSession session) {
        Employee employee = (Employee) session.getAttribute(Employee.SESSION_USER);
        banner.setCreateUserId(employee.getId());
        bannerService.save(banner);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的Banner
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", bannerService.findById(id));
    }

    /**
     * 修改banner信息
     *
     * @param banner
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Banner banner) {
        banner.setShowPlatform(StringUtils.arrayToCommaDelimitedString(banner.getShowPlatformArray()));
        bannerService.update(banner);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    /**
     * 删除Banner信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        bannerService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");

    }

}
